package com.hbase.common;

import java.io.IOException;

import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;

/**
 * hbase链接工厂
 * 
 * @desc:
 * @author wangsh
 * @date 2017-12-20 下午8:21:52
 */
public class HbaseConnectionFactory {

	private static ThreadLocal<HBaseConncetion> conncetions = new ThreadLocal<HBaseConncetion>();
	private static ThreadLocal<HBaseAdmin> admin = new ThreadLocal<HBaseAdmin>();

	/**
	 * 
	 * @param type
	 * @return
	 */
	public static HBaseConncetion getConnection() {
		HBaseConncetion conncetion = conncetions.get();
		if (conncetion == null) {
			synchronized (HbaseConnectionFactory.class) {
				if (conncetion == null) {
					conncetion = new HBaseConncetion();
					conncetions.set(conncetion);
				}
				return conncetion;
			}
		}
		return conncetion;
	}

	/**
	 * 
	 * @param type
	 * @return
	 * @throws IOException
	 * @throws ZooKeeperConnectionException
	 * @throws MasterNotRunningException
	 */
	public static HBaseAdmin getHBaseAdmin() throws Exception {
		HBaseAdmin hBaseAdmin = admin.get();
		if (hBaseAdmin == null) {
			synchronized (HbaseConnectionFactory.class) {
				if (hBaseAdmin == null) {
					hBaseAdmin = getConnection().getHBaseAdmin();
					admin.set(hBaseAdmin);
				}
				return hBaseAdmin;
			}
		}
		return hBaseAdmin;
	}

	public static void main(String[] args) {

	}
}
